package it.rap.applications.contributions.manager.configurator;

import it.rap.applications.container.IContainerPage;
import it.rap.applications.contributions.manager.Contributions;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public abstract class Configurator extends Bundle {

	  protected String configFilePath;
	  protected List<Bundle> bundles = new ArrayList<Bundle>();
	  
	  public Configurator() {
		super();
		setConfigFilePath();
		setBundles();
	}

	public void addContributions()  throws Exception {
		if ( bundles != null && bundles.size() > 0 ){
			for(Bundle bundle: bundles){
				Contributions.addContribution( bundle.getBundleId(), bundle.getContributionName(), getClass().getClassLoader().loadClass( bundle.getCoreClass() ).asSubclass(IContainerPage.class) );
			}
		}
	}
	  
	public void setBundles(){
		try {
			Properties prop = new Properties();
	
			InputStream inputStream = getClass().getClassLoader().getResourceAsStream( configFilePath );
	 
		    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		    factory.setValidating(false);
		    factory.setIgnoringElementContentWhitespace(true);
		    DocumentBuilder builder = factory.newDocumentBuilder();
	
	        Document doc = builder.parse( inputStream );
	        doc.getDocumentElement().normalize();
	        
	    	System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
	    	
	    	NodeList bundleList = doc.getElementsByTagName("bundle");
	    	if ( bundleList != null ){
		    	for (int i = 0; i < bundleList.getLength(); i++) {
		    		Bundle bundle = null;
		    		Node nNode = bundleList.item(i);    		
		    		if (nNode.getNodeType() == Node.ELEMENT_NODE) {    			 
		    			Element eElement = (Element) nNode;     
		    			String elementState =  eElement.getAttribute("state");
		    			if ( elementState != null && ON.equalsIgnoreCase( elementState ) ){
		    				bundle = new Bundle();
		    				bundle.setBundleId(  eElement.getAttribute("id") );
		    				bundle.setContributionName(  eElement.getAttribute("name") );
		    				
		    				NodeList nCoreCLasses = eElement.getElementsByTagName("coreClass");
		    				
		    					Node nCoreCLass = nCoreCLasses.item(0);    
		    				
		    					if (nCoreCLass.getNodeType() == Node.ELEMENT_NODE) { 
		    						Element cElement = (Element) nCoreCLass;     
		    		    				bundle.setCoreClass( cElement.getAttribute("name" ) );    		    				
		    		    			}
		    					}
		    				}
		    		if ( bundle != null  ){
		    			bundles.add( bundle );
		    		}
		    	}
	    		
	    	}
		} catch(Exception e){
			
		}
	}
	
	protected abstract void setConfigFilePath();
}
